;
(function (window, angular) {
    'use strict';
    angular.module("Controller.BasicData.replaceMaterial", [])
        .controller("ReplaceMaterialCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "EventBus", "UserContextService", "Upload", "ybConfig", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, EventBus, UserContextService, Upload, ybConfig) {
           
            $scope.activeFlag = [true, false];
 
            $scope.markInfo = {
                allChecked: false,
                curMaterialRow: -1,
                curLocationRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            
            $scope.submitted = false;
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.condition = {};
            $scope.dataList = [];
   
            // 替换类型
            $scope.replaceCodeList = [
                {code:'0',name:'单向'},
                {code:'1',name:'双向'},
            ];            
            
            $scope.tableConfig = {
                fixedWidth: 260,
                scrollWidth: 1270,
                tableCol: [
                    {
                        type: "",
                        name: "原零件号",
                        dataField: "sourceMaterialNo",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "原零件中文名",
                        dataField: "sourceMaterialDes",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "替换零件号",
                        dataField: "replaceMaterialNo",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "替换零件中文名",
                        dataField: "replaceMaterialDes",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "替换类型",
                        dataField: "replaceCodeShow",
                        width: 110,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "适用机型",
                        dataField: "machineType",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "image",
                        name: "零件图片",
                        dataField: "orgImageUrl",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "替换机构Id",
                        dataField: "replaceOrgId",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "替换机构名称",
                        dataField: "replaceOrgName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    }
                ]
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
            };

            $scope.handleBlur = function(key, e) {
                if (!e.target.value) {
                    delete $scope.condition[key];
                    return;
                }
                $scope.condition[key] = UserContextService.MultiInput(e.target.value).trim();
            };

            function getDataList(curPage) {
                $scope.markInfo.allChecked = false;
                $scope.markInfo.curMaterialRow = -1;
                var service = "device";
                var reqUrl = "/materials/queryReplaceMaterials";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;

                if ($scope.condition.sourceMaterialNosTemp) {
                    $scope.condition.sourceMaterialNos = $scope.condition.sourceMaterialNosTemp.split(',');
                } else {
                    $scope.condition.sourceMaterialNos = [];
                }

                let isExist = $scope.condition.sourceMaterialNosTemp && $scope.condition.sourceMaterialNosTemp.includes($scope.condition.sourceMaterialNo)
                
                if ($scope.condition.sourceMaterialNo && !isExist) {
                    $scope.condition.sourceMaterialNos.push($scope.condition.sourceMaterialNo);
                }

                const conditionList = ['sourceMaterialNos', 'replaceMaterialNos', 'replaceCode'];
                param.filters = {}; 
                conditionList.forEach(key => {
                    param.filters[key] = $scope.condition[key];
                })
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.replaceCodeShow = item.replaceCode == null ? '无替换关系' : (item.replaceCode== 0 ? '单向' : '双向');
                        if (item.orgImageUrl) {
                            item.orgImageUrlList = item.orgImageUrl.split(',').map(imageUrl => getImgUrlShow(imageUrl))
                        }
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index, type) {
                $scope.markInfo["cur" + type + "Row"] = index;
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewDetail":
                        viewDetail(item);
                        break;
                    default:
                        break;
                }
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            $scope.selectAll = function () {
                angular.forEach($scope.dataList, function (item) {
                    item.isChecked = $scope.markInfo.allChecked;
                });
            };

            function checkAll() {
                var selectAll = false;
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    if (!$scope.dataList[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectSingle = function (item) {
                if (!item.isChecked) {
                    $scope.markInfo.allChecked = false;
                } else {
                    if (!checkAll()) {
                        $scope.markInfo.allChecked = false;
                    } else {
                        $scope.markInfo.allChecked = true;
                    }
                }
            };
  
            $scope.viewImage = function (item) {
                ybWidget.ViewImage({
                    FileUrl: item
                })
            };

            function getImgUrlShow(val) {
                return ybConfig.getConfig().baseUrl.BjxlshFtp + val;
            }

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            (function () {
                getDataList();
            })();
        }])
        ;
})(window, window.angular);
